﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class jchaxun : System.Web.UI.Page
{
    private static string sqlconn = System.Configuration.ConfigurationManager.AppSettings["sqlcon"].ToString().Trim();
    private  string sql = "select * from J_TABLE where 1=1 ";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Binddate();
            gvjinhuo.Visible = false;
            Bindy();
            BindT();
        }
       
    }
    protected void Binddate()
    {
        users us = new users();
        gvjinhuo.DataSource = us.GetAlljinhuo(us);
        gvjinhuo.DataBind();
    }
    protected void drpjy_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    void Bindy()
    {
        users us = new users();    //绑定
        drpyname.DataSource = us.Bindyao().Tables[0].DefaultView;
        drpyname.DataTextField = "Y_NAME";
        drpyname.DataValueField = "Y_ID";
        drpyname.DataBind();

    }
    void BindT()
    {
        users us = new users();    //绑定
        drptname.DataSource = us.Binduser().Tables[0].DefaultView;
        drptname.DataTextField = "T_NAME";
        drptname.DataValueField = "T_ID";
        drptname.DataBind();

    }
    protected void drptname_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void btncha_Click(object sender, EventArgs e)
    {
       
        string dh = txthao.Text.Trim();
        string name = drpyname.SelectedValue.Trim();
        string tname = drptname.SelectedValue.Trim();
        string starttime = txttime.Text.Trim();
        string endtime = txttimee.Text.Trim();
        SqlConnection coon = new SqlConnection(sqlconn);
        try
        {
            coon.Open();
            SqlCommand comm = new SqlCommand(sql, coon);
            SqlDataReader rd = comm.ExecuteReader();
            if (rd.Read())
            {
                coon.Close();
                coon.Open();
               
                SqlDataAdapter adp = new SqlDataAdapter(sql, coon);
                try
                {
                    DataSet ds = new DataSet();
                    adp.Fill(ds, "J_TABLE");
                   
                    gvjinhuo.DataSource = ds.Tables[0].DefaultView;
                    gvjinhuo.DataBind();
                    gvjinhuo.Visible = true;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    coon.Close();
                }
            }
            else
            {
                Response.Write("<script>alert(\"对不起，查询不到该条件下的信息！\")</script>");
            }
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            coon.Close();
        }
        
　　   

    }
    protected void btncin_Click(object sender, EventArgs e)
    {
        txthao.Text = null;
        txttime.Text = null;
        txttimee.Text = null;
    }

    protected void chdh_CheckedChanged(object sender, EventArgs e)
    {
        string dh = txthao.Text.Trim();
        string name = drpyname.SelectedValue.Trim();
        string tname = drptname.SelectedValue.Trim();
        string starttime = txttime.Text.Trim();
        string endtime = txttimee.Text.Trim();
     
        if (chdh.Checked) sql += "and J_DANHAO ='" + dh + "'";
        if (ckyname.Checked) sql += "and J_NAME ='" + name + "'";
        if (chjname.Checked) sql += "and J_TNAME ='" + tname  + "'";
        if (chqtime.Checked) sql += "and J_TIME >='" + starttime  + "'";
        if (chjtime.Checked) sql += "and J_TIME <= '" + endtime + "'";
     
    }

    protected void gvjinhuo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvjinhuo.PageIndex = e.NewPageIndex;
        Binddate();
    }
    protected void gvjinhuo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvjinhuo.EditIndex = -1;
        Binddate();
    }
    
  
    protected void gvjinhuo_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvjinhuo.EditIndex = e.NewEditIndex;
        Binddate();
    }
    protected void gvjinhuo_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string dh = gvjinhuo.DataKeys[e.RowIndex].Value.ToString().Trim();
        string time = ((TextBox)(gvjinhuo.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim();
        string id = ((TextBox)(gvjinhuo.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim();
        string name = ((TextBox)(gvjinhuo.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim();
        string number = ((TextBox)(gvjinhuo.Rows[e.RowIndex].Cells[4].Controls[0])).Text.Trim();
        string price = ((TextBox)(gvjinhuo.Rows[e.RowIndex].Cells[5].Controls[0])).Text.Trim();
        string youxiaoqi = ((TextBox)(gvjinhuo.Rows[e.RowIndex].Cells[6].Controls[0])).Text.Trim();
        string gid = ((TextBox)(gvjinhuo.Rows[e.RowIndex].Cells[7].Controls[0])).Text.Trim();
        string tname = ((TextBox)(gvjinhuo.Rows[e.RowIndex].Cells[8].Controls[0])).Text.Trim();
        string ze = ((TextBox)(gvjinhuo.Rows[e.RowIndex].Cells[9].Controls[0])).Text.Trim();
        users us = new users();
        us.dh = dh;
        us.time = time;
        us.id = id;
        us.name = name;
        us.number = number;
        us.price = price;
        us.youxiaoqi = youxiaoqi;
        us.ze = ze;
        us.gid = gid;
        us.tname = tname;

        us.Updajinhuo(us);
        Binddate();
    }
    protected void gvjinhuo_Sorting(object sender, GridViewSortEventArgs e)
    {
        users us = new users();
        string sortExpression = e.SortExpression;
        if (gvjinhuo.SortDirection == SortDirection.Ascending)
        {
            DataView dv = us.GetAlljinhuo(us).Tables[0].DefaultView;
            dv.Sort = sortExpression + "DESC";
            gvjinhuo.DataSource = dv;
            gvjinhuo.DataBind();
        }
        else
        {
            DataView dv = us.GetAlljinhuo(us).Tables[0].DefaultView;
            dv.Sort = sortExpression + "ASC";
            gvjinhuo.DataSource = dv;
            gvjinhuo.DataBind();
        }
    }
    protected void gvjinhuo_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}